package com.htl.exampro.course.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.LocalDateTimeUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.htl.exampro.base.model.PageResult;
import com.htl.exampro.base.utils.UserUtil;
import com.htl.exampro.course.mapper.ChapterMapper;
import com.htl.exampro.course.mapper.CourseMapper;
import com.htl.exampro.course.pojo.domain.CourseDO;
import com.htl.exampro.course.pojo.domain.ChapterDO;
import com.htl.exampro.course.pojo.query.CoursePageParam;
import com.htl.exampro.course.pojo.vo.ChapterVO;
import com.htl.exampro.course.pojo.vo.CourseVO;
import com.htl.exampro.course.service.CourseService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 课程服务实现类
 */
@Service
public class CourseServiceImpl extends ServiceImpl<CourseMapper, CourseDO> implements CourseService {

    @Autowired
    private ChapterMapper chapterMapper;

    @Override
    public PageResult<CourseVO> getCoursePage(CoursePageParam pageParam) {
        LambdaQueryWrapper<CourseDO> wrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(pageParam.getCourseName())) {
            wrapper.like(CourseDO::getCourseName, pageParam.getCourseName())
                    .or().like(CourseDO::getCourseCode, pageParam.getCourseName());
        }
        wrapper.orderByDesc(CourseDO::getCreatedTime);
        Page<CourseDO> page = new Page<>(pageParam.getPageNumber(), pageParam.getPageSize());
        page(page, wrapper);
        return PageResult.result(page, CourseVO.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addCourse(CourseVO courseVO) {
        CourseDO courseDO = BeanUtil.toBean(courseVO, CourseDO.class);
        if (courseDO.getCourseCode() == null) {
            courseDO.setCourseCode(getCourseCode());
        }
        save(courseDO);
    }

    private String getCourseCode() {
        String userId = UserUtil.getUserId();
        return userId.substring(userId.length() - 4) + LocalDateTimeUtil.format(LocalDateTime.now(), "yyyyMMddHHmmssSSS");
    }

    @Override
    public CourseVO getChapter(String id) {
        return BeanUtil.toBean(getById(id), CourseVO.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateCourse(CourseVO course) {
        updateById(BeanUtil.toBean(course, CourseDO.class));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteCourse(String id) {
        removeById(id);
    }

    @Override
    public List<ChapterVO> getCourseChapters(String courseId) {
        LambdaQueryWrapper<ChapterDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(ChapterDO::getCourseId, courseId)
                .orderByAsc(ChapterDO::getSortOrder);
        List<ChapterDO> chapterDOS = chapterMapper.selectList(wrapper);
        return BeanUtil.copyToList(chapterDOS, ChapterVO.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addCourseChapter(ChapterVO chapter) {
        chapterMapper.insert(BeanUtil.toBean(chapter, ChapterDO.class));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateCourseChapter(ChapterVO chapter) {
        chapterMapper.updateById(BeanUtil.toBean(chapter, ChapterDO.class));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteCourseChapter(String id) {
        removeById(id);
    }
} 